var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);


router.post("/", function(req, res, next){
  var token = req.cookies.access_token;

  var option = {
    method: 'get',
    uri: config.apiHost+'/api/sysRoleMenu',
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      return res.json(result)
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.post("/addMenu", function(req, res, next){
  var token = req.cookies.access_token;

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/sysRoleMenu/addMenu',
    headers: {
      authorization: 'Bearer '+token
    },
    body: req.body,
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      return res.json(result)
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.post("/deleteMenu", function(req, res, next){
  var token = req.cookies.access_token;

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/sysRoleMenu/deleteMenu',
    headers: {
      authorization: 'Bearer '+token
    },
    body: req.body,
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      return res.json(result)
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.post("/queryByRole", function(req, res, next){
  var token = req.cookies.access_token;

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/sysRoleMenu/queryByRole',
    headers: {
      authorization: 'Bearer '+token
    },
    body: req.body,
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      return res.json(result)
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})


module.exports = router
